﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HandyControl.Controls;

namespace weighing
{
    /// <summary>
    /// BillNumberSettingWin.xaml 的交互逻辑
    /// </summary>
    public partial class BillNumberSettingWin : GlowWindow
    {
        #region variables
        private bool isSetDefaultValue;
        #endregion
        public BillNumberSettingWin()
        {
            InitializeComponent();

            CommonFunction.ShowCustomIcon(this);
        }

        #region win event
        private void GlowWindow_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void GlowWindow_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if(e.Key == Key.Escape) this.Close();
        }

        private void GlowWindow_ContentRendered(object sender, EventArgs e)
        {
            SetDefaultValue();
        }
        #endregion

        private void SetDefaultValue()
        {
            isSetDefaultValue = true;

            this.AutoGernertorCb.IsChecked = App.Config.BillPrintConfig.AutoGeneretor;
            this.headerTB.Text = App.Config.BillPrintConfig.Header;

            if (App.Config.BillPrintConfig.NumberMode == BillNumberMode.ChartAndNumber)
            {
                this.modeChartNumberRB.IsChecked = true;
            }
            else if (App.Config.BillPrintConfig.NumberMode == BillNumberMode.OnlyNumber)
            {
                this.modeNumberRB.IsChecked = true;
            }

            isSetDefaultValue = false;
        }


        #region 自动生成磅单


        private void AutoGernertorCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.BillPrintConfig.AutoGeneretor = true;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
            DelegeteFactory.Instance.OnBillSettingChanged?.Invoke();
        }

        private void AutoGernertorCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.BillPrintConfig.AutoGeneretor = false;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
            DelegeteFactory.Instance.OnBillSettingChanged?.Invoke();
        }

        #endregion

        #region 单号模式
        private void ModeNumberRB_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.BillPrintConfig.NumberMode = BillNumberMode.OnlyNumber;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
            DelegeteFactory.Instance.OnBillSettingChanged?.Invoke();
        }

        private void ModeChartNumberRB_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.BillPrintConfig.NumberMode = BillNumberMode.ChartAndNumber;
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
            DelegeteFactory.Instance.OnBillSettingChanged?.Invoke();
        }
        #endregion

        private void SaveBtn_Click(object sender, RoutedEventArgs e)
        {
            var header = this.headerTB.Text?.Trim();
            App.Config.BillPrintConfig.Header = header ?? "";
            CommonFunction.UpdateAppConfig(nameof(BillPrintConfig));
            Growl.Info("保存成功");
            this.saveBtn.IsChecked = false;
            DelegeteFactory.Instance.OnBillSettingChanged?.Invoke();
        }
    }
}
